//返现
exports.cashback = function(req,res) {

    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;
    var resultData = {};
    var oid  = 0;
    var is_cashback = 0;
    var cashback_money = 0;
    var cashback_min = 0;
    var cashback_max = 0;
    var o_money =0;
    var o_type = 0;
    var arr1 = [];
    var arr2 = [];
    var oc_id = 0;
    var oc_sales_volume = 0 //销量
    var o_wechat_pay = 0;
    var o_we_chat_no = 0;
    resultData.is_cash_back = 0;
    resultData.is_collect = 0;
    //任务流创建
    async.waterfall([

        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_order+"/o_no/" + bodyParams.o_no ,
                    method : 'POST',
                    data:{
                        fields:[
                            "o_oid", //机构id
                            "o_money", //订单金额
                            "o_type", //订单类型
                            "o_aoc_id", //订单类型
                            "o_wechat_pay", //微信支付金额
                            "o_we_chat_no" //微信支付金额
                        ].join(",")
                    }
                },
                function (err, data) {

                    oid = data.data[0].o_oid;
                    o_money = data.data[0].o_money;
                    o_type  = data.data[0].o_type;
                    oc_id = data.data[0].o_aoc_id;
                    o_wechat_pay = data.data[0].o_wechat_pay
                    o_we_chat_no = data.data[0].o_we_chat_no
                    cb();
                }
            );

        },
        //查询销量
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_organization_course+"/oc_id/" + oc_id ,
                    method : 'POST',
                    data:{
                        fields:[
                            "oc_sales_volume", //销量
                        ].join(",")
                    }
                },
                function (err, data) {
                    oc_sales_volume  = data.data[0].oc_sales_volume
                    cb();
                }
            );
        },
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchOrganization+"/o_id/" + oid ,
                    method : 'POST',
                    data:{
                        fields:[
                            "is_cashback", //是否开启返现活动  1 - 保底返现  2 - 阶梯返现
                            "cashback_money",//返现的标准价格
                            "cashback_min",//返现金额最小值 （阶梯金额）
                            "cashback_max"//返现金额的最大值 (返现金额)
                        ].join(",")
                    }
                },
                function (err, data) {

                    is_cashback = data.data[0].is_cashback;
                    cashback_money = data.data[0].cashback_money;
                    cashback_min = data.data[0].cashback_min;
                    cashback_max = data.data[0].cashback_max;
                    if(o_type == 2){//只有课程参加返现活动
                        if(is_cashback == 0){
                            return RES.response(res,false,"抱歉，该机构不参加返现活动");
                        }
                        if(is_cashback == 1){//机构参加的是保本返现活动
                            if(o_money < cashback_money){
                                return RES.response(res,false,"抱歉，该订单金额未达到返现");
                            }else{
                                if(oc_sales_volume%10 == 2 || oc_sales_volume%10 == 7){
                                    resultData.cash_back_cash = parseFloat(Math.random()*(210-200)+200).toFixed(2);
                                }else if(oc_sales_volume%10 == 5 || oc_sales_volume%10 == 9){
                                    resultData.cash_back_cash = parseFloat(Math.random()*(310-300)+300).toFixed(2);
                                }else{
                                    //获取到返现的金额
                                    resultData.cash_back_cash = parseFloat(Math.random()*(110-100)+100).toFixed(2);
                                }
                            }
                        }
                        if(is_cashback == 2){//参加的是阶梯返现
                            arr1 = common.strTransformArr(cashback_min);
                            arr2 = common.strTransformArr(cashback_max);
                            ////console.log(cashback_min);
                            ////console.log(arr1[1]);
                            if(o_money == arr1[0]){
                                resultData.cash_back_cash = arr2[0];
                            }else if(o_money== arr1[1] ){
                                resultData.cash_back_cash = arr2[1]
                            }else if(o_money == arr1[2]){
                                resultData.cash_back_cash = arr2[2]
                            }else{
                                return RES.response(res,false,"抱歉，该订单金额未达到返现");
                            }
                        }
                    }else{
                        return RES.response(res,false,"抱歉，活动订单不参与返现活动");
                    }
                    cb();
                }
            );
        },
        /*************************************/
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url: "http://localhost:3400/weixin/reimburse",
                    method: 'POST',
                    data: {
                        action: "tuikuan",
                        transaction_id: o_we_chat_no,
                        total_fee: o_money * 100,
                        refund_fee: resultData.cash_back_cash * 100
                    }

                },
                function (err, data) {
                    if(err){
                        return RES.response(res,false,data);
                    }
                   cb();
                }
            );
        },
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url: "http://127.0.0.1:3400/weixin/isAttention",
                    method: 'POST',
                    data: {
                        openid: bodyParams.openid
                    }

                },
                function (err, data) {
                    if(err){
                        return RES.response(res,false,data);
                    }
                    if(data.guanzhu){
                        resultData.is_collect = 1;
                    }
                    cb();
                }
            );
        },
        /************************************/
        //更新订单字段
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url:  CORE_APIConfig.coreAPI.update_order,
                    method: 'POST',
                    data: {
                        o_no: bodyParams.o_no,
                        o_is_cashback: 1,
                        o_cashback_money:resultData.cash_back_cash
                    }

                },
                function (err, data) {
                    if(err){
                        return RES.response(res,false,data);
                    }
                    cb();
                }
            );
        }



    ],function(err, result){
        //返回最终拼接结果
        RES.response(res,true,resultData);

    });
}